//
//  ZTDBManager.m
//  GyroOrientCalculate
//
//  Created by TangBin on 2020/1/26.
//  Copyright © 2020 TangBin. All rights reserved.
//

#import "ZTDBManager.h"
#import "ZHDBManager.h"

@implementation ZTDBManager

+ (instancetype)sharedInstance
{
    static ZTDBManager *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[[self class] alloc] init];
    });
    return _instance;
}

/// 点击通知
/// @param model model description
//- (void)addPushInfo:(XJWaresWanyModel *)model
//{
//    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
//    if (!queue)
//    {
//        NSLog(@"获取数据库连接池失败！");
//        return;
//    }
//
//    NSString *sql = [NSString stringWithFormat:@"insert into %@(id, uid, replyId, content, pid, username, type, photo, postsContent, homeImage, isRead, loginId)values('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', %d, '%@')", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%lld", model.id], [NSString stringWithFormat:@"%lld", model.uid], [NSString stringWithFormat:@"%ld", model.replyId], objectNull(model.content), [NSString stringWithFormat:@"%ld", model.pid], objectNull(model.username), objectNull(model.type), objectNull(model.photo), objectNull(model.postsContent), objectNull(model.homeImage), 0, [NSString stringWithFormat:@"%ld", model.loginId]];
//    debugLog(@"sql=%@", sql);
//    [queue inDatabase:^(FMDatabase * _Nonnull db) {
//        [db executeUpdate:sql];
////        [db executeUpdate:sql,
////         model.id, model.uid, model.replyId, objectNull(model.content), model.pid, objectNull(model.username), objectNull(model.type),
////         objectNull(model.photo), objectNull(model.postsContent), objectNull(model.homeImage), 0];
//    }];
//}

/*
 id integer NULL DEFAULT 0, \
 uid integer NULL DEFAULT 0, \
 replyId integer NULL DEFAULT 0, \
 content text NULL DEFAULT '', \
 pid integer NULL DEFAULT 0, \
 username text NULL DEFAULT '', \
 type varchar(10) NULL DEFAULT '0', \
 photo text NULL DEFAULT '', \
 postsContent text NULL DEFAULT '', \
 homeImage text NULL DEFAULT '', \
 isRead integer NULL DEFAULT 0, \
 create_time datetime DEFAULT CURRENT_TIMESTAMP \
 */

/// 把推送信息修改为已读状态
- (void)updatePushInfoRead:(NSInteger)loginId
{
    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
    if (!queue)
    {
        NSLog(@"获取数据库连接池失败！");
        return;
    }
    
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *strSql = [NSString stringWithFormat:@"update %@ set isRead=1 where isRead=0 and loginId='%@'", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)loginId]];
        [db executeUpdate:strSql];
    }];
    
}

// select COUNT(*) as tcount from T_DB_PUSH_CIRCLE_TABLE where isRead=0;
/// 获取未读通知数量
- (NSInteger)queryUnreadPushNum:(NSInteger)loginId
{
    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
    if (!queue)
    {
        NSLog(@"获取数据库连接池失败！");
        return 0;
    }
    __block NSInteger count = 0;
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *strSql = [NSString stringWithFormat:@"select COUNT(*) as tcount from %@ where isRead=0 and loginId='%@';", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)loginId]];
        FMResultSet *rs = [db executeQuery:strSql];
        while ([rs next])
        {
            count = [rs intForColumn:@"tcount"];
            break;
        }
        if (rs)
        {
            [rs close];
        }
    }];
    return count;
}

/// 读取数据
/// @param isRead NO 未读；YES所有
- (NSArray *)queryPushInfos:(BOOL)isRead loginId:(NSInteger)loginId
{
    return @[];
//    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
//    if (!queue)
//    {
//        NSLog(@"获取数据库连接池失败！");
//        return @[].copy;
//    }
//
//    NSMutableArray *addList = [NSMutableArray new];
//
//    [queue inDatabase:^(FMDatabase * _Nonnull db) {
//        FMResultSet *rs = nil;
//        NSString *strSql = [NSString stringWithFormat:@"select * from %@ where loginId='%@' order by push_id desc", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)loginId]];
//        if (!isRead)
//        {
//            strSql = [NSString stringWithFormat:@"select * from %@ where isRead=0 and loginId='%@' order by push_id desc", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)loginId]];
//        }
//
//        rs = [db executeQuery:strSql];
//        while ([rs next])
//        {
//            XJWaresWanyModel *model = [XJWaresWanyModel new];
//            NSString *idStr = [rs stringForColumn:@"id"];
//            model.id = [idStr longLongValue];
//            NSString *uid = [rs stringForColumn:@"uid"];
//            model.uid = [uid longLongValue];
//            NSString *replyId = [rs stringForColumn:@"replyId"];
//            model.replyId = [replyId longLongValue];
//            model.content = [rs stringForColumn:@"content"];
//            model.type = [rs stringForColumn:@"type"];
//            NSString *pid = [rs stringForColumn:@"pid"];
//            model.pid = [pid longLongValue];
//            model.createTime = [rs stringForColumn:@"create_time"];
//            model.photo = [rs stringForColumn:@"photo"];
//            model.postsContent = [rs stringForColumn:@"postsContent"];
//            model.homeImage = [rs stringForColumn:@"homeImage"];
//            model.username = [rs stringForColumn:@"username"];
//            [addList addObject:model];
//        }
//        if (rs)
//        {
//            [rs close];
//        }
//    }];
//
//    return addList.copy;
}

/// 清空所有推送信息
- (void)clearAllPushInfos:(NSInteger)loginId
{
    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
    if (!queue)
    {
        NSLog(@"获取数据库连接池失败！");
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"delete from %@ where loginId='%@'", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)loginId]];
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        [db executeUpdate:str];
    }];
}

/// 查询未读的最新的一条
/// @param loginId loginId description
//- (XJWaresWanyModel *)queryPushUnreadInfo:(NSInteger)loginId
//{
//    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
//    if (!queue)
//    {
//        NSLog(@"获取数据库连接池失败！");
//        return nil;
//    }
//    NSString *sql = [NSString stringWithFormat:@"SELECT * from %@ where isRead=0 and loginId='%@' order by push_id DESC LIMIT 1", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)loginId]];
//    XJWaresWanyModel *model = [XJWaresWanyModel new];
//    [queue inDatabase:^(FMDatabase * _Nonnull db) {
//        FMResultSet *rs = [db executeQuery:sql];
//        while ([rs next])
//        {
//            NSString *idStr = [rs stringForColumn:@"id"];
//            model.id = [idStr longLongValue];
//            NSString *uid = [rs stringForColumn:@"uid"];
//            model.uid = [uid longLongValue];
//            NSString *replyId = [rs stringForColumn:@"replyId"];
//            model.replyId = [replyId longLongValue];
//            model.content = [rs stringForColumn:@"content"];
//            model.type = [rs stringForColumn:@"type"];
//            NSString *pid = [rs stringForColumn:@"pid"];
//            model.pid = [pid longLongValue];
//            model.createTime = [rs stringForColumn:@"create_time"];
//            model.photo = [rs stringForColumn:@"photo"];
//            model.postsContent = [rs stringForColumn:@"postsContent"];
//            model.homeImage = [rs stringForColumn:@"homeImage"];
//            model.username = [rs stringForColumn:@"username"];
//        }
//        if (rs)
//        {
//            [rs close];
//        }
//    }];
//    return model;
//}

/// 删除帖子对应的所有的通知
/// @param pid pid description
- (void)deleteCirclePush:(NSInteger)pid
{
    FMDatabaseQueue *queue = [ZHDBManager getDBQueue];
    if (!queue)
    {
        NSLog(@"获取数据库连接池失败！");
        return;
    }
    NSString *strSql = [NSString stringWithFormat:@"DELETE from %@ where pid='%@'", T_DB_PUSH_CIRCLE_TABLE, [NSString stringWithFormat:@"%ld", (long)pid]];
    [queue inDatabase:^(FMDatabase * _Nonnull db) {
        [db executeUpdate:strSql];
    }];
}


@end

// SELECT * from T_DB_PUSH_CIRCLE_TABLE isRead=0 order by push_id DESC LIMIT 1;
